$file1 = $ARGV[0];
chomp($file1);
$count = $ARGV[1];
chomp($count);
open(BUFF,"samtools pileup $file1 |");
$prevchr = "chr1";
while($line = <BUFF>)
{
        chomp($line);
        @array = split(/\t/,$line);
        #print @array."\n";
        $chr = $array[0];
        $pos = $array[1];
	if($chr ne $prevchr)
	{
#		print $prevline."\n";
	}
	$prevchr = $chr;

	$prevline = $line;
}
 print $prevline."\n";
=head
open(BUF,"chr.txt");
while($line=<BUF>)
{
	chomp($line);
	print $line."\n";
	@chrarr = split(" ",$line);
	chomp($chrarr[0]);
	$chr{$chrarr[0]} = $chrarr[2];
}
$outfile = $file1;
$outfile =~ s/bam/txt/g;
#print $outfile."\n";
open(WRBUFF,">$outfile");
$line = <BUFF>;
 @array = split(/\t/,$line);
        #print @array."\n";
        $chr = $array[0];
	 chomp($chr);
	$finalpos =  $chr{$chr};
	print $finalpos."\n";
=head

        $pos = $array[1];
        $readcount = $array[3];
        $nextpos = $pos+$count;
	$binname = $chr."_".$pos."_".$nextpos;
	 $prevchr = $chr;
	$presentcount = $readcount;
while($line = <BUFF>)
{
	chomp($line);
	@array = split(/\t/,$line);
	#print @array."\n";
	$chr = $array[0];
	$pos = $array[1];
	$readcount = $array[3];
	
	if($chr ne $prevchr ||  $pos >  $nextpos)
	{
		print WRBUFF $binname."\t".$presentcount."\n";
		$nextpos = $nextpos+$count;
		$binname = $chr."_".$pos."_".$nextpos;
		 $presentcount = $readcount;
	}
	else
	{
		 $presentcount =  $presentcount + $pos;
	}	
	$prevchr = $chr;
}
